package controllers

import config.{Global, MyFile}
import javax.inject.Inject
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, Action, AnyContent, ControllerComponents}

import scala.concurrent.ExecutionContext

class DownloadController @Inject()(cc: ControllerComponents)(implicit exec: ExecutionContext) extends AbstractController(cc) with MyFile {

  def downloadPage = Action {
    Ok(views.html.download.downloadPage())
  }

  def downloadRnaSeqPage = Action{
    Ok(views.html.download.downloadRnaSeqPage())
  }

  def downloadFile(name: String, suffix: String) = Action { implicit request =>
    val s = suffix match {
      case "genome" => ".genome"
      case "gff" => ".gff"
      case "cds" => ".cds"
      case "pep" => ".pep"
      case "anno" => ".anno"
    }
    val n = name match {
      case "XJDY" => "XJDY"
      case "ZMNO" => "ZMNO"
      case "MSAE" => "MSAE"
      case "MTRU" => "MTRU"
      case "ZM-4" => "ZM-4"
    }
    Ok.sendFile(s"${Global.path}/fasta/$suffix/$n$s".toFile).withHeaders(
      //缓存
      CACHE_CONTROL -> "max-age=3600",
      CONTENT_DISPOSITION -> ("attachment; filename=" + n + s),
      "HttpResponse.entity.contentType" -> "application/x-download"
    )
  }

  def downloadExample(name: String) = Action { implicit request =>
    val file = name match {
      case "blastn" => "blastn_example.fasta"
      case "blastnGenome" => "blastn_example.fasta"
      case "blastp" => "blastp_example.fasta"
      case "muscle" => "muscle_example.fasta"
      case "lastz1" => "lastz1_example.fa"
      case "lastz2" => "lastz2_example.fa"
      case "grna" => "grna_example.fa"
      case "table1" => "table1.txt"
      case "table2" => "table2.txt"
    }
    Ok.sendFile(s"${Global.path}/example/$file".toFile).withHeaders(
      //缓存
      CACHE_CONTROL -> "max-age=3600",
      CONTENT_DISPOSITION -> ("attachment; filename=" + file),
      "HttpResponse.entity.contentType" -> "application/x-download"
    )
  }

  def getLastzExample(num: String): Action[AnyContent] = Action { implicit request =>
    val file = s"${Global.path}/example/lastz${num}_example.fa".toFile
    Ok(Json.toJson(file.readFileToString))
  }

  def downloadRNAseq(rnaType:String) = Action{implicit request=>

    val name = rnaType match{
      case "1" => "1_Roots_under_Al_treatment.txt"
      case "2" => "2_Leaves_under_cow_saliva_treatment.txt"
      case "3" => "3_Roots_under_ABA_treatment.txt"
      case "4" => "4_Roots_under_salt_and_mannitol_treatment.txt"
      case "5" => "5_Seedlings_under_cold treatment.txt"
      case "6" => "6_Leaves at different stages.txt"
      case "7" => "7_Different tissues of Medicago truncatula.txt"
      case "8" => "8_Roots infected with root-knot nematode.txt"
      case "9" => "9_27 diverse alfalfa genotypes.txt"
      case "10" => "10_Buds under salt stress.txt"
      case "11" => "11_Roots at seedling stage under Cd stress.txt"
      case "12" => "12_Roots at germination stage under salt stress.txt"
      case "13" => "13_Roots_at seedling stage under salt stress.txt"
      case "14" => "14_Different_defoliation_traits_(leaf).txt"
      case "15" => "15_Divergent_cell_wall_composition.txt"
      case "16" => "16_Roots_under_atrazine_treatment.txt"
      case "17" => "17_Roots_under_Pb_treatment.txt"
      case "18" => "18_Leaves_with_different_dormancy.txt"
    }

    Ok.sendFile(s"${Global.path}/RNA-seq/$rnaType.txt".toFile).withHeaders(
      //缓存
      CACHE_CONTROL -> "max-age=3600",
      CONTENT_DISPOSITION -> ("attachment; filename=" + name.replaceAll(" ","_")),
      "HttpResponse.entity.contentType" -> "application/x-download"
    )
  }

  def downloadTmp(path:String,file:String) = Action{implicit request=>
    Ok.sendFile(s"${Global.path}/tmp/$path/$file".toFile).withHeaders(
      //缓存
      CACHE_CONTROL -> "max-age=3600",
      CONTENT_DISPOSITION -> ("attachment; filename=" + file),
      "HttpResponse.entity.contentType" -> "application/x-download"
    )
  }

}
